package com.lkd.shoppingcart.dao.impl;

import java.util.List;

import com.lkd.shoppingcart.dao.ProductDao;
import com.lkd.shoppingcart.entity.Product;

public class ProductDaoImpl extends BaseDao implements ProductDao {

	@Override
	public int addProduct(Product product) {
		 String sql = "insert into product(name, price, counts, sales, productor, imgPath) values(?,?,?,?,?,?)";
	     return update(sql, product.getName(),product.getPrice(),product.getCounts(),
	    		 product.getSales(),product.getProductor(),product.getImgPath());
	}

	@Override
	public int deleteProductById(Integer id) {
		String sql = "delete from product where id = ?";
        return update(sql, id);
	}

	@Override
	public int updateProduct(Product product) {
        String sql = "update product set name=?,price=?,counts=?,sales=?,productor=?,imgPath=? where id = ?";
        return update(sql, product.getName(),product.getPrice(),product.getCounts(),
	    		 product.getSales(),product.getProductor(),product.getImgPath(), product.getId());
	}

	@Override
	public Product queryById(Integer id) {
		String sql = "select id, name, price, counts, sales, productor, imgPath, crtTime "
				+ "from product where id = ?";
        return queryForOne(Product.class, sql,id);
	}

	@Override
	public List<Product> queryAll() {
		String sql = "select id, name, price, counts, sales, productor, imgPath, crtTime from product";
        return queryForList(Product.class, sql);
	}

	@Override
	public Integer queryForTotalCount() {
		String sql = "select count(*) from product";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
	}

	@Override
	public List<Product> queryForPageItems(int begin, int pageSize) {
		String sql = "select id, name, price, counts, sales, productor, imgPath, crtTime "
				+ "from product limit ?,?";
		return queryForList(Product.class, sql, begin, pageSize);
	}

	@Override
	public Integer queryForTotalCountByPrice(int min, int max) {
		String sql = "select count(*) from product where price between ? and ?";
        Number count = (Number) queryForSingleValue(sql, min, max);
        return count.intValue();
	}

	@Override
	public List<Product> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
		String sql = "select id, name, price, counts, sales, productor, imgPath, crtTime "
				+ "from product where price between ? and ? order by price limit ?,?";
		return queryForList(Product.class, sql, min, max, begin, pageSize);
	}
	
}
